package com.yanzhu.service.impl;

import com.yanzhu.service.RechargeService;
import com.yanzhu.service.mapper.RechargeMapper;
import com.yanzhu.service.mapper.WalletMapper;
import com.yanzhu.service.model.Recharge;
import com.yanzhu.service.model.Wallet;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Author: 方炎柱
 * @Description:
 * @Date: Created in 21:45 2018/1/21
 * @Modified:
 */
@Service
@AllArgsConstructor
public class RechargeServiceImpl implements RechargeService {
    private RechargeMapper rechargeMapper;
    private WalletMapper walletMapper;

    @Override
    @Transactional
    public void insert(Recharge recharge) {
        rechargeMapper.insert(recharge);
        if(walletMapper.selectByUid(recharge.getUid()) != null){
            walletMapper.updateAfterRecharge(recharge.getUid(), recharge.getRecharge_fee());
        }else {
            Wallet wallet = new Wallet();
            wallet.setUid(recharge.getUid());
            wallet.setBalance(recharge.getRecharge_fee());
            walletMapper.insert(wallet);
        }

    }

    @Override
    public List<Recharge> queryRechargeList(long uid, int page, int size) {
        int begin = (page - 1) * size;
        return rechargeMapper.queryRechargeList(uid, begin, size);
    }

    @Override
    public int deleteRechargeById(long id) {
        return rechargeMapper.deleteRechargeById(id);
    }

    @Override
    public int deleteRechargeAll(long uid) {
        return rechargeMapper.deleteRechargeAll(uid);
    }
}
